package helper;

import java.security.MessageDigest;

/**
 * 安全帮助
 * */
public class SecurityHelper {
	/**
	 * MD5加密
	 * @param sourceStr 原字符串
	 * */
	public static String MD5(String dataStr, String charsetName) {
		   try {
	            MessageDigest md5 = MessageDigest.getInstance("MD5");
	            md5.update(dataStr.getBytes(charsetName));
	            byte digest[] = md5.digest();
	            StringBuilder sb = new StringBuilder();
	            for(int i = 0; i < digest.length; i++) {
	                sb.append(String.format("%02x", digest[i]));
	            }
	            return sb.toString();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return "";
	}
	/**
	 * byte 转 String
	 * */
	public static String byte2String(byte[] digest){
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < digest.length; i++) {
			String hv = Integer.toHexString((0x000000FF & digest[i]) | 0xFFFFFF00).substring(6);
			sb.append(hv);
		}
		return sb.toString();
	}
}
